<template>
  <div class="account-settings-info-view">
    <a-row :gutter="16">
      <a-form layout="vertical" :form="form">
        <a-col :md="24" :lg="16" :spinning="confirmLoading">
          <a-form-item label="账号">
            <a-input v-decorator.trim="['username']" :disabled="true"/>
          </a-form-item>
          <a-form-item label="姓名">
            <a-input v-decorator.trim="['realname',validatorRules.realname]"/>
          </a-form-item>
          <a-form-item label="性别">
            <j-dict-select-tag :triggerChange="true" v-decorator="['sex']" dictCode="sex"></j-dict-select-tag>
          </a-form-item>
          <a-form-item label="手机号" :required="false">
            <a-input v-decorator="['phone',validatorRules.phone]"/>
          </a-form-item>
          <a-form-item>
            <a-button @click="handleSubmit" type="primary" :loading="confirmLoading">更新信息</a-button>
          </a-form-item>
        </a-col>
        <a-col :md="24" :lg="8" :style="{ minHeight: '180px' }">
          <!--<div class="ant-upload-preview" @click="$refs.modal.edit(1)" >
            <a-icon type="cloud-upload-o" class="upload-icon"/>
            <div class="mask">
              <a-icon type="plus" />
            </div>
            <img :src="option.img"/>
          </div>-->
          <a-form-item label="头像">
            <j-image-upload class="appIconFile-uploader" text="上传" v-decorator="['avatar']" :disabled="false"></j-image-upload>
          </a-form-item>
        </a-col>
      </a-form>
    </a-row>

    <avatar-modal ref="modal">

    </avatar-modal>
  </div>
</template>

<script>
  import AvatarModal from './AvatarModal'
  import JDictSelectTag from '@comp/dict/JDictSelectTag'
  import JFormContainer from '@comp/jeecg/JFormContainer'
  import store from '@/store'
  import pick from 'lodash.pick';
  import { duplicateCheck } from '@api/api';
  import { USER_INFO } from '@/store/mutation-types';
  import Vue from 'vue'
  import { editUserBase, queryBaseUserById } from '../service';
  export default {
    components: {
      AvatarModal,
      JDictSelectTag,
      JFormContainer
    },
    data () {
      return {
        // cropper
        form: this.$form.createForm(this),
        confirmLoading: false,
        preview: {},
        model: {},
        option: {
          img: '/avatar2.jpg',
          info: true,
          size: 1,
          outputType: 'jpeg',
          canScale: false,
          autoCrop: true,
          // 只有自动截图开启 宽度高度才生效
          autoCropWidth: 180,
          autoCropHeight: 180,
          fixedBox: true,
          // 开启宽度和高度比例
          fixed: true,
          fixedNumber: [1, 1]
        },
        validatorRules: {
          realname: { rules: [{ required: true, message: '请输入姓名!' }] },
          phone: { rules: [{ required: true, message: '请输入手机号!' }, { validator: this.validatePhone }] }
        },
        url: {

        }
      }
    },
    created () {
      this.showCurrentUser();
    },
    methods: {
      showCurrentUser() {
        this.userId = store.getters.userInfo.id;
        queryBaseUserById(this.userId, {}).then((res) => {
          if (res.success) {
            this.form.resetFields();
            this.model = Object.assign({}, res.result);
            this.visible = true;
            this.$nextTick(() => {
              this.form.setFieldsValue(pick(this.model, 'username', 'realname', 'sex', 'phone', 'avatar'))
            })
          }
        });
      },
      handleSubmit () {
        let that = this;
        that.form.validateFields((err, values) => {
          if (!err) {
            if (that.model.id) {
              values.id = that.model.id;
            }
            editUserBase(values).then(res => {
              if (res.success) {
                let userInfo = Vue.ls.get(USER_INFO)
                userInfo.realname = res.result.realname;
                userInfo.avatar = res.result.avatar;
                Vue.ls.set(USER_INFO, userInfo, 7 * 24 * 60 * 60 * 1000);
                this.$store.commit('SET_AVATAR', userInfo.avatar);
                this.$store.commit('SET_NAME', {
                  username: userInfo.username,
                  realname: userInfo.realname,
                  welcome: ''
                });
                this.$message.success(res.message);
              } else {
                this.$message.error(res.message);
              }
            });
          }
        })
      },
      validatePhone(rule, value, callback) {
        if (!value) {
          callback()
        } else {
          if (new RegExp(/^1[3|4|5|7|8|9][0-9]\d{8}$/).test(value)) {
            let params = {
              tableName: 'sys_user',
              fieldName: 'phone',
              fieldVal: value,
              dataId: this.model.id,
              delFlag: 'del_flag',
              delFlagValue: 0
            };
            duplicateCheck(params).then((res) => {
              if (res.success) {
                callback()
              } else {
                callback('手机号已存在!')
              }
            })
          } else {
            callback('请输入正确格式的手机号码!');
          }
        }
      }
    }
  }
</script>

<style lang="less" scoped>

  .avatar-upload-wrapper {
    height: 200px;
    width: 100%;
  }

  .ant-upload-preview {
    position: relative;
    margin: 0 auto;
    width: 100%;
    max-width: 180px;
    border-radius: 50%;
    box-shadow: 0 0 4px #ccc;

    .upload-icon {
      position: absolute;
      top: 0;
      right: 10px;
      font-size: 1.4rem;
      padding: 0.5rem;
      background: rgba(222, 221, 221, 0.7);
      border-radius: 50%;
      border: 1px solid rgba(0, 0, 0, 0.2);
    }
    .mask {
      opacity: 0;
      position: absolute;
      background: rgba(0,0,0,0.4);
      cursor: pointer;
      transition: opacity 0.4s;

      &:hover {
        opacity: 1;
      }

      i {
        font-size: 2rem;
        position: absolute;
        top: 50%;
        left: 50%;
        margin-left: -1rem;
        margin-top: -1rem;
        color: #d6d6d6;
      }
    }

    img, .mask {
      width: 100%;
      max-width: 180px;
      height: 100%;
      border-radius: 50%;
      overflow: hidden;
    }
  }
</style>
